package com.entrogy.utils;

import com.alibaba.excel.EasyExcel;
import com.entrogy.bean.ImportCourseEntity;
import com.entrogy.bean.StudentEntity;
import com.entrogy.bean.StudentIcEntity;
import com.entrogy.listener.ImportCourseListener;
import com.entrogy.listener.StudentIcCardExcelListener;
import com.entrogy.listener.StudentScoreExcelListener;
import com.entrogy.listener.TeacherSalaryExcelListener;
import com.entrogy.service.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName EasyExcelTest
 * @Description
 * @Author luoxingjiang
 * @Date 2019/6/6 0006 下午 4:18
 * @Version 1.0
 **/
@Component
public class EasyExcelUtil {

    // 解析学生分数的方法
    public Integer resolveExcelWithStudent(InputStream inStream, StudentScoreService studentScoreService, ExamService examService, Integer examId) throws Exception {
        InputStream inputStream = inStream;
        try {
            try {
                // 解析每行结果在listener中处理
                StudentScoreExcelListener listener = new StudentScoreExcelListener(studentScoreService, examService, examId);
                EasyExcel.read(inputStream, listener).sheet().headRowNumber(2).doRead();
                return listener.getExamId();
            } finally {
                inputStream.close();
            }
        }catch (Exception e){
            throw e;
        }
    }

    // 解析工资条数据的方法
    public boolean resolveExcelWithSalary(InputStream inStream, Integer salaryYear, Integer salaryMonth, Integer templateId, String createUserId, Integer schoolCode, SalarySlipService salarySlipService) {
        InputStream inputStream = inStream;
        try {
            // 解析每行结果在listener中处理
            TeacherSalaryExcelListener teacherSalaryExcelListener = new TeacherSalaryExcelListener(salaryYear, salaryMonth, templateId, createUserId, schoolCode, salarySlipService);
            EasyExcel.read(inputStream, teacherSalaryExcelListener).sheet().headRowNumber(2).doRead();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    // 解析学生绑定IC卡excel信息
    public List<StudentEntity> resolveExcelWithStudentIcCard(InputStream inStream, StudentService studentService) {
        try {
            // 解析每行结果在listener中处理
            StudentIcCardExcelListener studentIcCardExcelListener = new StudentIcCardExcelListener(studentService);
            EasyExcel.read(inStream, StudentIcEntity.class, studentIcCardExcelListener).sheet().doRead();
            List<StudentEntity> failBindStudentList = studentIcCardExcelListener.getFailBindStudentList();
            studentIcCardExcelListener.setFailBindStudentList(new ArrayList<>());
            return failBindStudentList;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public List<ImportCourseEntity> resolveExcelWithCourse(String fileName, CourseSystemService courseSystemService, UtilsService utilsService, RestTemplate restTemplate) {
        ImportCourseListener importCourseListener = new ImportCourseListener(courseSystemService, utilsService, restTemplate);
        EasyExcel.read(fileName, ImportCourseEntity.class, importCourseListener).sheet().doRead();
        return importCourseListener.getFailImportCourseList();
    }

    // 解析选课信息并导入数据库
//    public void resolveExcelWithCourse(String fileName, CourseSystemService courseSystemService) {
//        EasyExcel.read(fileName, ImportCourseEntity.class, new ImportCourseListener(courseSystemService)).sheet().doRead();
//    }
}
